#!/bin/bash
# 北交所录入助审系统 - Docker离线部署脚本
# 用于在目标服务器上导入和运行Docker镜像

set -e

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 配置变量
IMAGE_NAME="bjs-luruzhushen"
IMAGE_TAG="latest"
CONTAINER_NAME="bjs-luruzhushen-app"
PORT=8580

echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}北交所录入助审系统 - Docker离线部署${NC}"
echo -e "${GREEN}========================================${NC}"

# 检查Docker是否安装
if ! command -v docker &> /dev/null; then
    echo -e "${RED}错误: Docker未安装，请先安装Docker${NC}"
    exit 1
fi

# 检查Docker服务是否运行
if ! docker info &> /dev/null; then
    echo -e "${RED}错误: Docker服务未运行，请启动Docker${NC}"
    exit 1
fi

# 查找镜像文件
echo -e "\n${YELLOW}查找镜像文件...${NC}"
IMAGE_FILE=""

if [ -f "${IMAGE_NAME}-${IMAGE_TAG}.tar" ]; then
    IMAGE_FILE="${IMAGE_NAME}-${IMAGE_TAG}.tar"
elif [ -f "${IMAGE_NAME}-${IMAGE_TAG}.tar.gz" ]; then
    echo -e "${YELLOW}解压镜像文件...${NC}"
    tar -xzf "${IMAGE_NAME}-${IMAGE_TAG}.tar.gz"
    IMAGE_FILE="${IMAGE_NAME}-${IMAGE_TAG}.tar"
else
    echo -e "${RED}错误: 找不到镜像文件 ${IMAGE_NAME}-${IMAGE_TAG}.tar[.gz]${NC}"
    exit 1
fi

echo -e "${GREEN}✓ 找到镜像文件: ${IMAGE_FILE}${NC}"

# 步骤1: 导入Docker镜像
echo -e "\n${YELLOW}[步骤 1/4] 导入Docker镜像...${NC}"
docker load -i "${IMAGE_FILE}"

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ Docker镜像导入成功${NC}"
else
    echo -e "${RED}✗ Docker镜像导入失败${NC}"
    exit 1
fi

# 步骤2: 停止并删除旧容器（如果存在）
echo -e "\n${YELLOW}[步骤 2/4] 检查旧容器...${NC}"
if [ "$(docker ps -aq -f name=${CONTAINER_NAME})" ]; then
    echo -e "${YELLOW}停止旧容器...${NC}"
    docker stop ${CONTAINER_NAME} || true
    echo -e "${YELLOW}删除旧容器...${NC}"
    docker rm ${CONTAINER_NAME} || true
    echo -e "${GREEN}✓ 旧容器已清理${NC}"
else
    echo -e "${GREEN}✓ 无旧容器需要清理${NC}"
fi

# 步骤3: 创建必要的目录
echo -e "\n${YELLOW}[步骤 3/4] 创建数据目录...${NC}"
mkdir -p logs data/uploads data/temp
echo -e "${GREEN}✓ 数据目录创建完成${NC}"

# 步骤4: 启动新容器
echo -e "\n${YELLOW}[步骤 4/4] 启动容器...${NC}"
docker run -d \
    --name ${CONTAINER_NAME} \
    -p ${PORT}:8580 \
    -v $(pwd)/logs:/app/logs \
    -v $(pwd)/data/uploads:/app/data/uploads \
    -v $(pwd)/data/temp:/app/data/temp \
    -e HOST=0.0.0.0 \
    -e PORT=8580 \
    -e DEBUG=false \
    -e LOG_LEVEL=INFO \
    --restart unless-stopped \
    ${IMAGE_NAME}:${IMAGE_TAG}

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ 容器启动成功${NC}"
else
    echo -e "${RED}✗ 容器启动失败${NC}"
    exit 1
fi

# 等待服务启动
echo -e "\n${YELLOW}等待服务启动...${NC}"
sleep 5

# 检查容器状态
if [ "$(docker ps -q -f name=${CONTAINER_NAME})" ]; then
    echo -e "${GREEN}✓ 容器运行中${NC}"
    
    # 尝试健康检查
    echo -e "\n${YELLOW}执行健康检查...${NC}"
    for i in {1..10}; do
        if curl -f http://localhost:${PORT}/health &> /dev/null; then
            echo -e "${GREEN}✓ 服务健康检查通过${NC}"
            break
        else
            if [ $i -eq 10 ]; then
                echo -e "${YELLOW}⚠ 健康检查失败，请检查日志${NC}"
            else
                echo -e "${YELLOW}等待服务就绪... ($i/10)${NC}"
                sleep 2
            fi
        fi
    done
else
    echo -e "${RED}✗ 容器未运行${NC}"
    echo -e "${YELLOW}查看容器日志:${NC}"
    docker logs ${CONTAINER_NAME}
    exit 1
fi

# 显示结果
echo -e "\n${GREEN}========================================${NC}"
echo -e "${GREEN}部署完成！${NC}"
echo -e "${GREEN}========================================${NC}"

echo -e "\n${YELLOW}服务信息:${NC}"
echo -e "  容器名称: ${CONTAINER_NAME}"
echo -e "  访问地址: http://localhost:${PORT}"
echo -e "  API文档: http://localhost:${PORT}/docs"
echo -e "  健康检查: http://localhost:${PORT}/health"

echo -e "\n${YELLOW}常用命令:${NC}"
echo -e "  查看日志: docker logs -f ${CONTAINER_NAME}"
echo -e "  停止服务: docker stop ${CONTAINER_NAME}"
echo -e "  启动服务: docker start ${CONTAINER_NAME}"
echo -e "  重启服务: docker restart ${CONTAINER_NAME}"
echo -e "  删除容器: docker rm -f ${CONTAINER_NAME}"

echo -e "\n${GREEN}全部完成！✨${NC}"

